﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.ApplicationServer.Caching;
using AppFabricSample.Data;
using System.Threading;

namespace AppFabricWeb
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnGetStudentMark_Click(object sender, EventArgs e)
        {
            // DataCacheFactory will use settings from app.config
            using (DataCacheFactory dataCacheFactory = new DataCacheFactory())
            {


                // Get a default cache client
                DataCache dataCache = dataCacheFactory.GetCache("TestCache");


                // Get the cached object back from the cache
                Student cachedPerson = (Student)dataCache.Get(CacheKey<Student>.ForStudentByName("Ram"));
                txtStudentMarkInital.Text= cachedPerson.Name + ": " + cachedPerson.Marks.ToString();
                cachedPerson.Marks = 15;
                
                // Update it in Cache
                dataCache.Put(CacheKey<Student>.ForStudentByName("Ram"), cachedPerson);

            }

        }

        void SleepForMin(int min)
        {
            txtStudentMarkAfterExpire.Text =String.Format("Sleeping for {0:00.02} MIN STARTED", min.ToString());
            for (int i = 1; i <= (min); i++)
            {
                Thread.Sleep(1000 * 60);
                txtStudentMarkAfterExpire.Text =String.Format("DONE: {0:00.02} MIN", i.ToString());
            }
            txtStudentMarkAfterExpire.Text = String.Format("Sleeping for {0:00.02}  MIN END", min.ToString());
        }

        protected void btnGetStudentMarkAfterChange_Click(object sender, EventArgs e)
        {
            // DataCacheFactory will use settings from app.config
            using (DataCacheFactory dataCacheFactory = new DataCacheFactory())
            {


                // Get a default cache client
                DataCache dataCache = dataCacheFactory.GetCache("TestCache");

                //Read it from Cache
                Student cachedPerson1 = (Student)dataCache.Get(CacheKey<Student>.ForStudentByName("Ram"));
                txtStudentMarkAfterChange.Text = cachedPerson1.Name + ": " + cachedPerson1.Marks.ToString();


            }
        }

        protected void btnGetStudentMarkAfterExpire_Click(object sender, EventArgs e)
        {

            // DataCacheFactory will use settings from app.config
            using (DataCacheFactory dataCacheFactory = new DataCacheFactory())
            {


                // Get a default cache client
                DataCache dataCache = dataCacheFactory.GetCache("TestCache");


                // Get the cached object back from the cache
               

                SleepForMin(3);
                Student cachedPerson2 = (Student)dataCache.Get(CacheKey<Student>.ForStudentByName("Ram"));
                txtStudentMarkAfterExpire.Text = cachedPerson2.Name + ": " + cachedPerson2.Marks.ToString();


            }

        }
    }
}